package com.gagakuai.bpm.busniess.run;



import com.gagakuai.bpm.domian.dto.response.ResponseStatusDto;
import com.gagakuai.bpm.domian.entity.camuda.TaskInfoEntity;
import com.gagakuai.bpm.domian.events.CallBusinessTaskCompleteEvent;
import com.gagakuai.bpm.rpcservice.openfeign.UserHttpApi;
import com.gagakuai.bpm.utils.StringUtils;
import com.gagakuai.bpm.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.history.HistoricTaskInstance;
import org.camunda.bpm.engine.task.Task;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class CallBusinessTaskCompleteRun implements Runnable{
    @Resource
    private UserHttpApi userHttpApi;

    @Resource
    private HistoryService historyService;

    @Resource
    private TaskService taskService;

    private CallBusinessTaskCompleteEvent callBusinessTaskCompleteEvent;

    @Override
    public void run() {
        UserUtil.setToken(callBusinessTaskCompleteEvent.getToken());

        if(StringUtils.isBlank(callBusinessTaskCompleteEvent.getTaskId())){
            log.info("流程走完回调接口：参数【taskId】 为空");
            return;
        }

        HistoricTaskInstance  taskInstance = historyService.createHistoricTaskInstanceQuery().taskId(callBusinessTaskCompleteEvent.getTaskId()).singleResult();
        if(ObjectUtils.isEmpty(taskInstance)){
            log.info("流程走完回调接口：未找到历史任务实例：{}", callBusinessTaskCompleteEvent.getTaskId());
            return;
        }

        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(taskInstance.getProcessInstanceId()).singleResult();
        if(ObjectUtils.isEmpty(taskInstance)){
            log.info("流程走完回调接口：未找到流程实例实例：{}", callBusinessTaskCompleteEvent.getTaskId());
            return;
        }

        // 获取下一个节点产生的任务
        List<TaskInfoEntity> taskInfoEntityList = new ArrayList<>();
        List<Task> tasks = taskService.createTaskQuery().processInstanceId(historicProcessInstance.getRootProcessInstanceId()).list();
        tasks.forEach(t -> {
            TaskInfoEntity taskInfoEntity = new TaskInfoEntity();
            taskInfoEntity.setId(t.getId());
            taskInfoEntity.setName(t.getName());
            taskInfoEntity.setActivityId(t.getTaskDefinitionKey());
            taskInfoEntityList.add(taskInfoEntity);
        });

        ResponseStatusDto<Void> responseStatusDto = userHttpApi.updateProcActivityInfos(historicProcessInstance.getBusinessKey(),
                taskInfoEntityList);
        UserUtil.removeToken();
        log.info("流程走完回调接口：{}，{}", responseStatusDto.getCode(), responseStatusDto.getMessage());
    }

    public void setCallBusinessTaskCompleteEvent(CallBusinessTaskCompleteEvent callBusinessTaskCompleteEvent){
        this.callBusinessTaskCompleteEvent = callBusinessTaskCompleteEvent;
    }
}
